// pages/index/realMsg/realMsg.js
const http = require('../../../utils/http.js')
const md5 = require('../../../utils/md5.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      cameraDirection:true,
      imgPath: ['1','1','1'],
      imgPaths: ['1', '1', '1'],
      camera:false,
      phoneNum:'',
      isSet:false,
      isClick:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      
  },
   btnGo(){
       let _this = this
       if (this.data.isClick) {
           
            let data={
                method:'Auth.realName',
                token:wx.getStorageSync('token'),
                front: _this.data.imgPath[1], 
                aspect: _this.data.imgPath[2],
                face: _this.data.imgPath[0]
            }            
            let arr = http.sorts(data)
            data.sign = md5.hex_md5(arr)
           console.log(data)
           wx.showLoading({
               title: '正在上传...',
           })
            http._post(data,res=>{
                console.log(res)
                if(res.data.code==0){
                     wx.hideLoading()  
                    wx.navigateTo({
                        url: '/pages/index/realMsg/realSuccess/realSuccess'
                    })
                }else{
                    wx.hideLoading() 
                    wx.showToast({
                        title: res.data.msg,
                        icon:'none',
                        duration:1000
                    })
                }
            })
      
          
        }   
   },
   //调起相机
    addPersonImg(e){   
        console.log()    
        this.setData({
            camera: true,
            phoneNum: e.currentTarget.dataset.selset
        })
       
    },
    //关闭相机
    closeCamera(){
        this.setData({
            camera: false
        })
    },
    //切换摄像头
    devicePosition(){     
            this.setData({
                cameraDirection: !this.data.cameraDirection
            })    
    },
    //点击拍照
    photoClick(){
        let _this = this;
        let arrImg=[];
        let typeNum = _this.data.phoneNum;
        if (_this.data.phoneNum==0){
            typeNum=3
        } 
        const ctx = wx.createCameraContext()
        ctx.takePhoto({
            quality: 'high',
            success: (res) => {
                _this.data.imgPaths[_this.data.phoneNum] = res.tempImagePath;
                _this.setData({
                    imgPaths: _this.data.imgPaths
                })   
                setTimeout(() => {
                    this.setData({
                        camera: false
                    })
                }, 300)
                console.log('类型'+typeNum)
                let data = {
                    method: 'File.uploadImg',
                    token: wx.getStorageSync('token'),
                    type: typeNum,
                }
                let arr = http.sorts(data)
                data.sign = md5.hex_md5(arr)
                wx.uploadFile({
                    url: http.http,
                    filePath: res.tempImagePath,
                    name: 'file',
                    formData:data,success(val) {                        
                        console.log(JSON.parse(val.data).data.url)
                        if (JSON.parse(val.data).code == 0) {
                            _this.data.imgPath[_this.data.phoneNum] = JSON.parse(val.data).data.url;   
                            _this.setData({
                                imgPath: _this.data.imgPath
                            })                            
                            if (_this.data.imgPath.indexOf('1') == -1) {
                                console.log(_this.data.imgPath)
                                _this.setData({ isSet: true, isClick: true })
                            }
                        } else {
                            wx.showToast({
                                title: '失败',
                                icon: 'none',
                                duration: 2000
                            })
                        }
                    }
                })
              
               
            }
        })
       
              
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})